package com.cloud.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.annex.AnnexUrlService;
import com.cloud.common.constants.GlobalConstants;
import com.cloud.common.enums.BannerStatus;
import com.cloud.enums.LoginDevice;
import com.cloud.service.RedisService;
import com.cloud.system.entity.ApplyBanner;
import com.cloud.system.mapper.SysApplyBannerMapper;
import com.cloud.system.service.ISysApplyBannerService;
import com.cloud.utils.SecurityUtils;
import com.cloud.utils.StringUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 应用轮播图
 *
 * @author yzj
 * @since 2022-08-12
 */
@Service
public class SysApplyBannerServiceImpl extends ServiceImpl<SysApplyBannerMapper, ApplyBanner> implements ISysApplyBannerService {

    @Resource
    private SysApplyBannerMapper sysApplyBannerMapper;

    @Resource
    private RedisService redisService;

    @Resource
    private AnnexUrlService annexUrlService;

    /**
     * 应用轮播图分页
     *
     * @author yzj
     */
    @Override
    public List<ApplyBanner> selectSysApplyBannerList(ApplyBanner applyBanner) {
        List<ApplyBanner> applyBannerList = sysApplyBannerMapper.selectSysApplyBannerList(applyBanner);
        if (applyBannerList.size() > 0) {
            for (ApplyBanner bannerInfo : applyBannerList) {
                applyBanner.setImageUrl(annexUrlService.getImageTurnPath(bannerInfo.getImageUrl()));
            }
        }
        return applyBannerList;
    }

    /**
     * 应用轮播图新增
     *
     * @author yzj
     */
    @Override
    public int insertObject(ApplyBanner applyBanner) {
        return sysApplyBannerMapper.insert(applyBanner);
    }

    /**
     * 应用轮播图修改
     *
     * @author yzj
     */
    @Override
    public int updateObject(ApplyBanner applyBanner) {
        return sysApplyBannerMapper.updateById(applyBanner);
    }

    /**
     * 刷新轮播图缓存
     */
    @Override
    public void clearCache() {
        String sysCode = SecurityUtils.getSysCode();
        // 删除轮播图缓存信息
        redisService.delHashKey(GlobalConstants.applyBannerMap, sysCode);
        // 重新设置轮播图缓存
        ApplyBanner applyBanner = new ApplyBanner();
        applyBanner.setSysCode(sysCode);
        applyBanner.setStatus(BannerStatus.ENABLE.getCode());
        List<ApplyBanner> applyBannerList = sysApplyBannerMapper.selectSysApplyBannerList(applyBanner);
        if (applyBannerList.size() > 0) {
            invertBannerPath(applyBannerList);
            redisService.setHash(GlobalConstants.applyBannerMap, sysCode, applyBannerList);
        }
    }

    /**
     * 设置轮播图开启状态
     *
     * @param bannerId
     * @return
     */
    @Override
    public int setUpBanner(Long bannerId) {
        return sysApplyBannerMapper.updateBannerStatus(BannerStatus.ENABLE.getCode(), bannerId);
    }

    /**
     * 关闭轮播图状态
     *
     * @param bannerId
     * @return
     */
    @Override
    public int closeUpBanner(Long bannerId) {
        return sysApplyBannerMapper.updateBannerStatus(BannerStatus.DISABLE.getCode(), bannerId);
    }

    /**
     * 查询开启状态轮播列表
     *
     * @return
     */
    @Override
    public List<ApplyBanner> getAllApplyBannerList() {
        String sysCode = SecurityUtils.getSysCode();
        List<ApplyBanner> applyBannerList = redisService.getHashKey(GlobalConstants.applyBannerMap, sysCode);
        if (StringUtils.isNull(applyBannerList)) {
            ApplyBanner applyBanner = new ApplyBanner();
            applyBanner.setSysCode(sysCode);
            applyBanner.setStatus(BannerStatus.ENABLE.getCode());
            // 查询为空缓存重新设置缓存
            applyBannerList = sysApplyBannerMapper.selectSysApplyBannerList(applyBanner);
            if (applyBannerList.size() > 0) {
                invertBannerPath(applyBannerList);
                redisService.setHash(GlobalConstants.applyBannerMap, sysCode, applyBannerList);
            }
        }
        return applyBannerList;
    }

    /**
     * 应用轮播图详情
     *
     * @param bannerId
     * @return
     */
    @Override
    public ApplyBanner getObjectInfo(Long bannerId) {
        ApplyBanner applyBanner = sysApplyBannerMapper.selectById(bannerId);
        if (StringUtils.isNotNull(applyBanner)) {
            applyBanner.setImageUrl(annexUrlService.getImageTurnPath(applyBanner.getImageUrl()));
        }
        return applyBanner;
    }

    /**
     * 应用图片访问路径转化
     *
     * @param applyBannerList
     */
    private void invertBannerPath(List<ApplyBanner> applyBannerList) {
        if (applyBannerList.size() > 0) {
            for (ApplyBanner applyBanner : applyBannerList) {
                applyBanner.setImageUrl(annexUrlService.getImageOrVideoPath(applyBanner.getImageUrl(),
                        LoginDevice.APP.getCode()));
            }
        }
    }

}
